import { ref } from "vue";
import { defineStore } from "pinia";
import AppModel from "@/model/app";
import tool from "@/helper/tool";
import bus from "vue3-eventbus";

export const useAppStore = defineStore("app", () => {
  const showLogin = ref(false);
  const nowApp = ref<any>({});
  const openAppList = ref<any[]>([]);
  const dockAppList = ref<any[]>([]);
  const openWidgetList = ref<any[]>([]);
  const volumn = ref(80);
  const launchpad = ref(false);

  /**
   * @description: 设置全局音量
   */
  function setVolumn(_volumn) {
    volumn.value = _volumn;
  }
  /**
   * @description: 退出登录
   */
  function logout() {
    nowApp.value = false;
    openAppList.value = [];
    showLogin.value = true;
  }
  /**
   * @description: 登录
   */
  function login() {
    showLogin.value = false;
  }
  /**
   * @description: 打开上一次的应用
   */
  function openTheLastApp() {
    for (let i = openAppList.value.length - 1; i >= 0; i--) {
      if (!openAppList.value[i].hide) {
        showApp(openAppList.value[i]);
        break;
      }
    }
  }
  /**
   * @description: 最小化应用
   */
  function hideApp(app) {
    for (let i in openAppList.value) {
      if (openAppList.value[i].pid == app.pid) {
        openAppList.value[i].hide = true;
        break;
      }
    }
    openTheLastApp();
  }
  /**
   * @description: 根据PID关闭应用
   */
  function closeWithPid(pid) {
    for (let i in openAppList.value) {
      if (openAppList.value[i].pid == pid) {
        openAppList.value.splice(parseInt(i), 1);
        break;
      }
    }
    for (let i in dockAppList.value) {
      if (dockAppList.value[i].pid == pid && !dockAppList.value[i].keepInDock) {
        dockAppList.value.splice(parseInt(i), 1);
        break;
      }
    }
  }
  /**
   * @description: 关闭应用
   */
  function closeApp(app) {
    if (app.hideWhenClose) {
      hideApp(app);
    } else {
      for (let i in openAppList.value) {
        if (app.pid) {
          if (openAppList.value[i].pid == app.pid) {
            openAppList.value.splice(parseInt(i), 1);
            break;
          }
        } else {
          if (openAppList.value[i].key == app.key) {
            openAppList.value.splice(parseInt(i), 1);
            break;
          }
        }
      }
      if (!app.keepInDock) {
        for (let i in dockAppList.value) {
          if (app.pid) {
            if (dockAppList.value[i].pid == app.pid) {
              dockAppList.value.splice(parseInt(i), 1);
              break;
            }
          } else {
            if (dockAppList.value[i].key == app.key) {
              dockAppList.value.splice(parseInt(i), 1);
              break;
            }
          }
        }
      }
      openTheLastApp();
    }
  }
  /**
   * @description: 打开应用
   */
  function openApp(app) {
    if (launchpad.value) {
      launchpad.value = false;
    }
    if (app.outLink) {
      app.url && window.open(app.url);
      return;
    }
    app.hide = false;
    let isExist = false;
    for (let i in openAppList.value) {
      if (openAppList.value[i].key == app.key) {
        isExist = true;
        break;
      }
    }
    if (isExist) {
      showApp(app);
    } else {
      app.pid =
        new Date().valueOf() + "." + Math.ceil(Math.random() * 99999999);
      app = JSON.parse(JSON.stringify(app));
      openAppList.value.push(app);
      let isExistDock = false;
      for (let i in dockAppList.value) {
        if (dockAppList.value[i].key == app.key) {
          //dock里已经有相同的应用了 不push
          isExistDock = true;
          break;
        }
      }
      if (!isExistDock) {
        dockAppList.value.push(app);
      }
    }
    nowApp.value = JSON.parse(JSON.stringify(app));
  }
  /**
   * @description: 显示并置顶APP
   */
  function showApp(app) {
    let _openAppList = JSON.parse(JSON.stringify(openAppList.value));
    for (let i in _openAppList) {
      if (_openAppList[i].pid == app.pid) {
        _openAppList.splice(i, 1);
        break;
      }
    }
    app.hide = false;
    app = JSON.parse(JSON.stringify(app));
    _openAppList.push(app);
    openAppList.value = _openAppList;
    nowApp.value = app;
  }
  /**
   * @description: 根据key打开APP
   */
  function openAppByKey(key) {
    let app = tool.getAppByKey(key);
    if (app) {
      openApp(app);
    }
  }
  /**
   * @description: 带参数打开App
   */
  function openWithData(data) {
    data.app.data = data.data;
    openApp(data.app);
  }
  /**
   * @description: 获取常驻Dock的App列表
   */
  function getDockAppList() {
    let arr = <any[]>[];
    let appList = AppModel.allAppList;
    for (let app of appList) {
      if (app.keepInDock) {
        app.pid =
          new Date().valueOf() + "." + Math.ceil(Math.random() * 99999999);
        arr.push(app);
      }
    }
    dockAppList.value = arr;
  }
  function openMenu(key) {
    switch (key) {
      case "close":
        closeApp(nowApp.value);
        break;
      default: //默认通过事件总线发送
        bus.emit(key);
        break;
    }
  }
  function launchpadToggle() {
    launchpad.value = !launchpad.value;
  }

  return {
    showLogin,
    nowApp,
    openAppList,
    dockAppList,
    openWidgetList,
    volumn,
    launchpad,
    setVolumn,
    logout,
    login,
    openTheLastApp,
    hideApp,
    closeWithPid,
    closeApp,
    openApp,
    showApp,
    openAppByKey,
    openWithData,
    getDockAppList,
    openMenu,
    launchpadToggle,
  };
});
